package sk.gtug.hackatlon.drunkdroid;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

public class Activity3 extends Activity {

	private Button[] myButtons;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.screen3);
		this.myButtons = new Button[3];
		this.myButtons[0] = (Button) this.findViewById(R.id.hitter);
		this.myButtons[1] = (Button) this.findViewById(R.id.hitter2);
		this.myButtons[2] = (Button) this.findViewById(R.id.hitter3);
		this.myButtons[0].setEnabled(true);
		this.myButtons[1].setEnabled(false);
		this.myButtons[2].setEnabled(false);
		for (Button bt : this.myButtons) {
			bt.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Handler handler = new Handler();
					new WaitingThread(handler, Activity3.this.myButtons).start();
				}
			});
		}

	}
}

class WaitingThread extends Thread {

	Handler handler;
	private Button[] buttons;

	public WaitingThread(Handler handler, Button[] buttons) {
		super();
		for(Button button : buttons)
		{
			button.setEnabled(false);
		}
		this.buttons = buttons;
		this.handler = handler;
	}

	@Override
	public void run() {
		try {
			sleep(600);
		} catch (InterruptedException e) {
		}
		this.handler.post(new FinishThread(this.buttons));
	}
}

class FinishThread implements Runnable {

	private Button[] buttons;

	public FinishThread(Button[] buttons) {
		this.buttons = buttons;
	}

	@Override
	public void run() {
		Random randomGenerator = new Random();
		int randomInt = randomGenerator.nextInt(2);
		this.buttons[randomInt].setEnabled(true);
	}
}